package com.qf.java2201.controller;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.read.builder.ExcelReaderBuilder;
import com.alibaba.excel.read.builder.ExcelReaderSheetBuilder;
import com.alibaba.excel.write.builder.ExcelWriterBuilder;
import com.alibaba.excel.write.builder.ExcelWriterSheetBuilder;
import com.qf.java2201.common.PageVo;
import com.qf.java2201.common.R;
import com.qf.java2201.dto.NoticeExcelDto;
import com.qf.java2201.listener.WebNoticeListener;
import com.qf.java2201.pojo.Notice;
import com.qf.java2201.service.NoticeService;
import org.apache.commons.io.FileUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/notice")
@CrossOrigin("*")
public class NoticeController {

    @Autowired
    private NoticeService noticeService;

    @RequestMapping("/noticePageData")
    public PageVo<Notice> getPageData(@RequestParam Map<String, Object> params){
        return noticeService.getPageData(params);
    }

    @RequestMapping( "/addNotice")
    public R addNotice(Notice notice){
        int result = noticeService.addNotice(notice);
        R<Object> r = new R<>();
        if (result == 1){
            r.setCode(0);
            r.setMsg("添加成功");
        }else {
            r.setCode(-1);
            r.setMsg("添加失败");
        }
        return r;
    }

    @RequestMapping("/deleteNotice")
    public R deleteNotice(Integer id){
        int result = noticeService.deleteNotice(id);
        R<Object> r = new R<>();
        if (result == 1){
            r.setCode(0);
            r.setMsg("删除成功");
        }else {
            r.setCode(-1);
            r.setMsg("删除失败");
        }
        return r;
    }


    @RequestMapping("/updateNotice")
    public R updateNotice(Notice notice){
        int result = noticeService.updateNotice(notice);
        R<Object> r = new R<>();
        if (result == 1){
            r.setCode(0);
            r.setMsg("修改成功");
        }else {
            r.setCode(-1);
            r.setMsg("修改失败");
        }
        return r;
    }

    //导出
    @RequestMapping("/export")
    public void export(Notice notice, HttpServletResponse response) throws IOException {
        //导出学生数据
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        //防止中文乱码
        String filename = URLEncoder.encode("公告信息导出","utf-8");

        response.setHeader("Content-Disposition","attachment;filename="+filename+".xlsx");

        ServletOutputStream outputStream = response.getOutputStream();

        ExcelWriterBuilder write = EasyExcel.write(outputStream,Notice.class);
        ExcelWriterSheetBuilder sheet = write.sheet();

        //准备列表数据
        List<Notice> notices = noticeService.selectList(notice);

        sheet.doWrite(notices);
    }

    //模板下载
    @RequestMapping("/templateDownload")
    public void templateDowload(HttpServletRequest request,HttpServletResponse response) throws IOException {
        //下载  模板
        //设置响应数据的类型
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");

        //读取服务器的模板文件 转为 字节数组
        String path = request.getServletContext().getRealPath("/template/公告信息模板.xlsx");
        File file = new File(path);
        //把文件对象 转为 字节数组对象
        byte[] bytes = FileUtils.readFileToByteArray(file);

        //防止中文乱码
        String filename = URLEncoder.encode("公告信息导出表","UTF-8");

        response.setHeader("Content-Disposition","attachment;filename="+filename+".xlsx");

        ServletOutputStream outputStream = response.getOutputStream();

        outputStream.write(bytes);

        outputStream.close();
    }

    @Autowired
    private WebNoticeListener webNoticeListener;


    //公告信息导入
    @RequestMapping("/import")
    public R importExcelData(MultipartFile file) throws IOException {
        //上传 excel 表格
        ExcelReaderBuilder read = EasyExcel.read(file.getInputStream(), NoticeExcelDto.class, webNoticeListener);

        ExcelReaderSheetBuilder sheet = read.sheet();

        sheet.doRead();

        return R.ok();


    }





}
